﻿using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Win7_Taskbar_Progressbar
{
    class Win7TaskbarProgressbar
    {
        ContainerControl ownerForm;

        public enum ProgressState
        {
            NoProgress = 0,
            Indeterminate = 0x1,
            Normal = 0x2,
            Error = 0x4,
            Paused = 0x8
        }

        public void SetBar(int value, int maximum)
        {
            SetProgressValue(ownerForm.Handle, (ulong)value, (ulong)maximum);
        }

        public void SetBar(ulong value, ulong maximum)
        {
            SetProgressValue(ownerForm.Handle, value, maximum);
        }
        public void SetBar(ProgressState state)
        {
            SetProgressState(ownerForm.Handle, state);
        }


        [GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")]
        [ClassInterfaceAttribute(ClassInterfaceType.None)]
        [ComImportAttribute()]
        internal class CTaskbarList { }
        private static ITaskbarList3 _taskbarList;
        internal static ITaskbarList3 TaskbarList
        {
            get
            {
                if (_taskbarList == null)
                {
                    lock (typeof(Win7TaskbarProgressbar))
                    {
                        if (_taskbarList == null)
                        {
                            _taskbarList = (ITaskbarList3)new CTaskbarList();
                            _taskbarList.HrInit();
                        }
                    }
                }
                return _taskbarList;
            }
        }
        public Win7TaskbarProgressbar() { }

        public Win7TaskbarProgressbar(ContainerControl parentControl)
        {
            ContainerControl = parentControl;
        }
        public ContainerControl ContainerControl
        {
            get { return ownerForm; }
            set
            {
                ownerForm = value;
            }
        }

        private static void SetProgressState(IntPtr hwnd, ProgressState state)
        {
            if (Windows7OrGreater)
                TaskbarList.SetProgressState(hwnd, state);
        }

        private static void SetProgressValue(IntPtr hwnd, ulong current, ulong maximum)
        {
            if (Windows7OrGreater)
                TaskbarList.SetProgressValue(hwnd, current, maximum);
        }


        static readonly OperatingSystem osInfo = Environment.OSVersion;
        internal static bool Windows7OrGreater
        {
            get
            {
                return (osInfo.Version.Major == 6 && osInfo.Version.Minor >= 1)
                    || (osInfo.Version.Major > 6);
            }
        }

        [ComImportAttribute()]
        [GuidAttribute("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
        [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface ITaskbarList3
        {
            [PreserveSig]
            void HrInit();
            [PreserveSig]
            void AddTab(IntPtr hwnd);
            [PreserveSig]
            void DeleteTab(IntPtr hwnd);
            [PreserveSig]
            void ActivateTab(IntPtr hwnd);
            [PreserveSig]
            void SetActiveAlt(IntPtr hwnd);
            [PreserveSig]
            void MarkFullscreenWindow(
                IntPtr hwnd,
                [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
            void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
            void SetProgressState(IntPtr hwnd, ProgressState tbpFlags);
        }
    }
}
